package Test;

 import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class SimpleAnalyzer {
	// Constructor parameters
	private File inputFile;
	private int windowSize;
	
	// send windowSize to TextGenerater
	
	
	
	
	// Data structure for the analysis
	HashMap<String, Integer> analysis = new HashMap<String, Integer>();
	
	public SimpleAnalyzer(File inputFile, int windowSize) {
		this.inputFile = inputFile;
		this.windowSize = windowSize;
		
	}
	
	public void analyze() throws IOException {
		int intChar = -1;
		
		// Set everything up
		char[] window = new char[windowSize]; // Our window onto the text
		Reader streamIn = new BufferedReader(new FileReader(inputFile));
		analysis.clear();
		
	
		char firstletter = 0;
			
		// Fill the window for the first time
		for (int i = 0; i < windowSize; i++) {
			intChar = streamIn.read();
			window[i] = (char) intChar;
		    firstletter = window[0];
		    
		
			
		}
		TextGenerater TextGenerater = new TextGenerater(firstletter);
		// Loop: read a character, update the window, update the analysis
		while (intChar != -1) {
			// Add current window to the analysis
			String key = new String(window);
			if (analysis.containsKey(key)) {
				// already present, so add one to existing count
				Integer oldValue = analysis.get(key);
				analysis.put(key, new Integer(oldValue + 1));
			} else {
				// new combination, add for the first time
				analysis.put(key, new Integer(1));
			}
			
			// Shift the window by one character
			for (int i = 0; i < (windowSize-1); i++) {
				window[i] = window[i+1];
			}
			
			// Read the next character
			intChar = streamIn.read();
			if (intChar > -1) {
				window[windowSize-1] = (char) intChar;
			}
		}
		
		
	TextGenerater.gen2(analysis);
	}
	
	
	
	public void printResults() {
		// To iterate over a map, we must first get a list of all valid keys
		Set<String> keySet = analysis.keySet();
		
		Iterator<String> i = keySet.iterator();
		while (i.hasNext()) {
			String key = i.next();
			int value = analysis.get(key);
		//	System.out.println("Key '" + key + "' occurs " + value + " times");
			/*if(key.substring(1,2).equals("T")){
				System.out.println(key+value);
			}*/
		}
	}
}
